package org.proddevplm.dao.core;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.proddevplm.dao.api.IProjectMgmt;
import org.proddevplm.dao.data.Project;
import org.proddevplm.dao.data.RelationMatrix;
import org.proddevplm.dao.data.Roles;
import org.proddevplm.dao.data.User;
import org.proddevplm.dao.util.HibernateUtil;

class ProjectMgmt implements IProjectMgmt{

	@Override
	public boolean addProject(Project project, User user, Roles role) {
		
		RelationMatrix relMatrix = new RelationMatrix();
		relMatrix.setProject(project);
		relMatrix.setRole(role);
		relMatrix.setUser(user);
		HibernateUtil.currentSession().persist(relMatrix);
		
		return false;
	}

	@Override
	public boolean editProject(int projectID, Project project, User user, Roles role) {
		
		RelationMatrix editRelMatrix = new RelationMatrix();
		editRelMatrix.setProject(project);
		editRelMatrix.setRole(role);
		editRelMatrix.setUser(user);
		HibernateUtil.currentSession().merge(editRelMatrix);
		
		return false;
	}

	@Override
	public boolean deleteProject(int projectID, Project project) {
		
		return false;
	}

	@Override
	public List<Project> getProjectByUserId(int userId) {
		
		Session session = HibernateUtil.currentSession();
		
		Query q = session.createQuery("select r from RelationMatrix r where r.user.id=:userId");
		q.setInteger("userId", userId);
		List<RelationMatrix> matrixResults = q.list();
		List<Project> projectResults = new ArrayList<Project>(matrixResults.size());
		
		for (RelationMatrix r:matrixResults)
			projectResults.add(r.getProject());
		
		
		return projectResults;
		
	}

	@Override
	public List<RelationMatrix> getRelMatrixByProjectId(int projectId) {
		
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select r from RelationMatrix r where r.project.id=:projectId");
		q.setInteger("projectId", projectId);
		List<RelationMatrix> relMatrixResults = q.list();
		
		return relMatrixResults;
	}

	@Override
	public List<RelationMatrix> getRelMatrixByUserId(int userId) {

		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select r from RelationMatrix r where r.user.id=:userId");
		q.setInteger("userId", userId);
		List<RelationMatrix> relMatrixResults = q.list();
		
		return relMatrixResults;
	}

	@Override
	public Project getProjectByProjectId(int projectId) {

		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select p from Project p where p.id=:projectId");
		q.setInteger("projectId", projectId);
		
		Project currentProject = (Project)q.uniqueResult();
		 		
		return currentProject;
	}

	@Override
	public boolean deleteRelMatrixByProjectId(int projectId) {

		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("delete from RelationMatrix r where r.project.id=:projectId");
		q.setInteger("projectId", projectId);
		q.executeUpdate();
		return false;
	}

	@Override
	public RelationMatrix getRelMatrixByProjUserRole(int projectId, int userId,
			int roleId) {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select r from RelationMatrix r where r.project.id=:projectId and r.user.id=:userId and r.role.id=:roleId");
		q.setInteger("projectId", projectId);
		q.setInteger("userId", userId);
		q.setInteger("roleId", roleId);
		return (RelationMatrix)q.uniqueResult();
	}

	@Override
	public boolean changeRelMatrixByRole(RelationMatrix relMatrix, Roles role) {
		
		relMatrix.setRole(role);
		HibernateUtil.currentSession().merge(relMatrix);
		return false;
	}
}
